<?php

if(!defined('IN_AACMS') || !defined('IN_ADMINCP')) {
	exit('Access Denied');
}

$purl = 'action=sgpage&operation=list&page='.($_GET['page']?$_GET['page']:1);

if($operation == 'list') {

	if(submitcheck('listsubmit')) {
		
		$ids = dimplode($_POST['ids']);
				
		if(!$ids) {
			cpmsg('article_choose_at_least_one_sgpage', $purl, 'error');
		}
		if(!$_POST['optype']) {
			cpmsg('article_choose_at_least_one_operation', $purl, 'error');
		}
		
		if($_POST['optype'] == 'delete') {
			include_once libfile('function/sgpagecp');
			delete_sgpage($ids);
			cpmsg('sgpage_delete_succeed', $purl, 'succeed');
		}
		
	}
		
	$perpage = 10;
	$page = $_GET['page'] ? max(intval($_GET['page']), 1) : 1;
	$start = ($page - 1) * $perpage;
	
	$count = DB::getOne("SELECT COUNT(*) FROM ".DB::table('sgpage'));				
	$page_list = DB::getAll("SELECT * FROM ".DB::table('sgpage')." ORDER BY pid DESC LIMIT $start,$perpage");
	
	$multipage = multi($count, $perpage, $page, ADMINSCRIPT.'?action=sgpage&operation=list');
	
	include template('sgpage_list', 'admin');
	
} elseif($operation == 'add') {
	
	if(submitcheck('sgpagesubmit')) {
			
		if(!$_POST['title']) {
			cpmsg('sgpage_title_empty', $purl, 'error');
		}
		
		if($_POST['conver']) {
			$conver = unserialize(stripslashes($_POST['conver']));
			$_POST['pic'] = addslashes($conver['pic']);
			$_POST['remote'] = intval($conver['remote']);
		}
		
		$_POST['username'] = $_SESSION['username'];
		$_POST['dateline'] = TIMESTAMP;
		
		if($pid = DB::insert('sgpage', $_POST)) {
			
			if($_POST['attach_ids']) {
				$newaids = array();
				$_POST['attach_ids'] = explode(',', $_POST['attach_ids']);
				foreach ($_POST['attach_ids'] as $newaid) {
					$newaid = intval($newaid);
					if($newaid) $newaids[] = $newaid;
				}
				if($newaids) {
					DB::update('sgpage_attachment', array('pid'=>$pid), "attachid IN (".dimplode($newaids).") AND pid='0'");
				}
			}
			cpmsg('sgpage_add_succeed', $purl, 'succeed');
			
		} else {		
			cpmsg('sgpage_add_error', $purl, 'error');
		}
		
	}
	
	include libfile('class/editor');
	$editor = editor::instance();
	$content_editor = $editor->create('content');
	
	include template('sgpage_form', 'admin');
	
} elseif($operation == 'edit') {
	
	$pid = intval($_GET['pid']);

	if(submitcheck('sgpagesubmit')) {
	
		if(!$_POST['title']) {
			cpmsg('sgpage_title_empty', $purl, 'error');
		}
		
		if($_POST['conver']) {
			$conver = unserialize(stripslashes($_POST['conver']));
			$_POST['pic'] = addslashes($conver['pic']);
			$_POST['remote'] = intval($conver['remote']);
		}
		
		if(DB::update('sgpage', $_POST, "pid='$pid'")) {
			
			if($_POST['attach_ids']) {
				$newaids = array();
				$_POST['attach_ids'] = explode(',', $_POST['attach_ids']);
				foreach ($_POST['attach_ids'] as $newaid) {
					$newaid = intval($newaid);
					if($newaid) $newaids[] = $newaid;
				}
				if($newaids) {
					DB::update('sgpage_attachment', array('pid'=>$pid), "attachid IN (".dimplode($newaids).") AND pid='0'");
				}
			}
			
			cpmsg('sgpage_edit_succeed', $purl, 'succeed');
		} else {
			cpmsg('sgpage_edit_error', $purl, 'error');
		}
		
	}
	
	
	$page = DB::getRow("SELECT * FROM ".DB::table('sgpage')." WHERE pid='$pid'");	
	
	include libfile('class/editor');
	$editor = editor::instance();
	$content_editor = $editor->create('content', $page['content']);
	
	$page['attach_image'] = $page['attach_file'] = '';
	$query = DB::query("SELECT * FROM ".DB::table('sgpage_attachment')." WHERE pid='$pid' ORDER BY attachid DESC");
	while ($value = DB::fetch($query)) {
		if($value['isimage']) {
			if($article['pic']) {
				$value['pic'] = $article['pic'];
			}
			$page['attach_image'] .= get_uploadcontent($value, 'sgpage');
		} else {
			$page['attach_file'] .= get_uploadcontent($value, 'sgpage');
		}
	}
	
	include template('sgpage_form', 'admin');
	
} elseif($operation == 'delete') {

	$pid = intval($_GET['pid']);
	include_once libfile('function/sgpagecp');
	delete_sgpage(array($pid));
	cpmsg('sgpage_delete_succeed', $purl, 'succeed');
	
}







?>